//
//  CYPostedOnlineDetailVC.m
//  CreditAddressBook
//
//  Created by LE on 16/1/4.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYPostedOnlineDetailVC.h"
#import "CYPostedOnlineDetailTVC.h"
#import "CYJumpsTool.h"
#import "CYAppealView.h"
#import "CYAppealVC.h"
#import "CYNetworkCollectRecord.h"
#import "CYLoginHttpTool.h"
#import "CYShareTool.h"
#import "CYDialTool.h"
#import "UIImage+DY.h"

@interface CYPostedOnlineDetailVC ()

@property(nonatomic, weak) CYPostedOnlineDetailTVC *tableview;
@end

@implementation CYPostedOnlineDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTable];
    self.title = @"详情";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupShareBtn];
}

- (void)setupTable {
    //申诉view
    CYAppealView *appealView = [CYAppealView loadFromNibUsingClassName];
    appealView.y = LENavigationH;
    [appealView.appealBtn addTarget:self action:@selector(appealBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:appealView];
    //tableview
    CYPostedOnlineDetailTVC *detail = [CYPostedOnlineDetailTVC le_loadFromStoryBoard:[CYJumpsTool mainSB]];
    detail.record = self.record;
    detail.view.y = LENavigationH + appealView.height;
    detail.view.height = CYScreeSize.height - LENavigationH - appealView.height;
    [self addChildViewController:detail];
    [self.view addSubview:detail.view];
    [detail didMoveToParentViewController:self];
    
    self.tableview = detail;
    
    appealView.hidden = YES;
    [detail loadCompletion:^(CYNetworkCollectRecord *record) {
        appealView.hidden = NO;
    }];
}
/**
 *  申诉
 */
- (void)appealBtnClick {
    CYUser *user = [CYLoginHttpTool userData].user;
    if (user) {
        CYAppealVC *appeal = [[CYAppealVC alloc] init];
        appeal.defrId = self.record.recordId;
        [self.navigationController pushViewController:appeal animated:YES];
    } else {
        [CYJumpsTool jumpToLogin:self];
    }
}

- (void)setupShareBtn {
    UIBarButtonItem *share = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(shareItem:)];
    self.navigationItem.rightBarButtonItem = share;
}

- (void)shareItem:(UIBarButtonItem *)item {
//    UIImage *shareImage = [self.tableview cropTable];
    
    UIImage *shareImage = [CYDialTool imageFromView:self.view];
//    shareImage = [shareImage croppedImageWithCGRect:CGRectMake(0, 64, shareImage.size.width, shareImage.size.height - 64)];
    [CYShareTool shareImage:shareImage controller:self completion:^(BOOL isSuccess) {
        if (isSuccess) {
            
        }
    }];
}

- (void)dealloc {
    CYLog(@"网络曝光销毁");
}
@end
